# coding=utf-8

from lib.tornado_routes import route
from web.handler import BaseHandler
from tornado import gen
from tornado.web import asynchronous
from web.models.user.thumb.index import Index
from decorator.auth import front_login_required
# from decorator.auth import admin_login_required, admin_auth


@route(r'/user/student/?', name='user.student')
class IndexHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        school_id = self.get_argument('schoolId', default=self.school_id)
        Fposition=self.get_argument('pn',default=2)
        Fdorm_id=self.get_argument('dorm_id',default=1)
        Fstudent_id = self.get_scookie()['user']['Fid']
        staff, total = yield Index().select_staff(school_id, Fstudent_id, 0, self.offset,Fposition,Fdorm_id)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1
        self.render("user/thumb/index.html", data=staff, page=total,Fposition=Fposition)


@route(r'/user/student/list?', name='user.student_list')
class ListHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        cur_page = self.get_argument('page', default=1)
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        staff, total = yield Index().select_staff(school_id, Fstudent_id, int(cur_page) - 1, self.offset)

        if (float(total) / self.offset * 10 % 10) > 0:
            total = total / self.offset + 1

        self.render("user/thumb/list.html", data=staff, page=total)


@route(r'/user/student/status?', name='user.student_status')
class StatusHandler(BaseHandler):

    @asynchronous
    @gen.coroutine
    @front_login_required
    def get(self):
        Fcrew_id = self.get_argument('p', default=0)
        school_id = self.get_argument('schoolId', default=self.school_id)

        Fstudent_id = self.get_scookie()['user']['Fid']
        ret = yield Index().set_status(Fstudent_id, Fcrew_id, school_id)
        self.render("user/thumb/status.html", data=ret)
